package cn.tansanqinger.dao.sql.knowledge;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.tansanqinger.dao.sql.IKnowledgeSqlDao;
import cn.tansanqinger.pojo.Knowledge;

@Repository
public class KnowledgeSqlDaoImpl {
	
	@Autowired
	private IKnowledgeSqlDao dao;
	
	/**
	 * 查询数据，通过条件查询
	 * @param grade
	 * @param keywords
	 * @param page
	 * @param limit
	 * @return
	 */
	public Map<String, Object> selectMessage(String keywords, int kispopup, int option, int page, int limit){
		QueryWrapper<Knowledge> queryWrapper = new QueryWrapper<Knowledge>();
		if(!"".equals(keywords)) {
			queryWrapper.and(wrapper -> wrapper.like("ksn", keywords).or().like("ktitle", keywords));
		}
		if(kispopup!=-1) {
			queryWrapper.lambda().eq(Knowledge::getKispopup, kispopup);
		}
//		List<Map<String, Object>> list = dao.listKnowledgeSelect();
//		for (int i = 0; i < list.size(); i++) {
//			System.err.println(list.get(i));
//		}
		if(option==1) {
			queryWrapper.eq("kaudit", 1).eq("kisreport", 0).eq("kisquery", 0);
		} else if(option==2) {
			queryWrapper.eq("kaudit", 0);
		} else if(option==3) {
			queryWrapper.eq("kisreport", 1);
		} else if (option==4) {
			queryWrapper.eq("kisquery", 1);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("count", dao.selectCount(queryWrapper));
		Page<Knowledge> pg = new Page<Knowledge>(page, limit);
		IPage<Knowledge> iPage = dao.selectPage(pg, queryWrapper);
		List<Knowledge> coursesList = iPage.getRecords();
		resultMap.put("data", coursesList);
		resultMap.put("code","0");
		resultMap.put("msg", "查询成功");
		return resultMap;
	}
	
	
	
	/**
	 * 查询数据，查询全部数据
	 * @return
	 */
	public List<Knowledge> selectMessageAll(){
		return dao.selectList(null);
	}
	
	/**
	 * 获取编号
	 * @return
	 */
	public Integer serialNumber() {
		QueryWrapper<Knowledge> queryWrapper = new QueryWrapper<Knowledge>();
		queryWrapper.orderByDesc("ksn").last("limit 1");
		Knowledge object= dao.selectOne(queryWrapper);
		return object==null?10000001:object.getKsn()+1;
	}
	
	/**
	 * 添加信息
	 * @param course
	 * @return
	 */
	public int add(Knowledge object) {
		return dao.insert(object);
	}
	
	/**
	 * 查询信息
	 * @param cid
	 * @return
	 */
	public Knowledge selectOnt(int id) {
		return dao.selectById(id);
	}
	
	/**
	 * 查询信息
	 * @param csn
	 * @return
	 */
	public Knowledge selectSn(int id) {
		QueryWrapper<Knowledge> queryWrapper = new QueryWrapper<Knowledge>();
		queryWrapper.lambda().eq(Knowledge::getKsn, id);
		return dao.selectOne(queryWrapper);
	}
	
	/**
	 * 修改状态
	 * @param cid
	 * @param isuse
	 * @return
	 */
	public int updateStatus(Knowledge object) {
		return dao.updateById(object);
	}
	
	/**
	 * 删除信息
	 * @param cid
	 * @return
	 */
	public int deleteOne(int id) {
		return dao.deleteById(id);
	}
	
	/**
	 * 修改信息
	 * @param user
	 * @return
	 */
	public int modification(Knowledge object) {
		return dao.updateById(object);
	}

}
